package com.yaorange.teaching.service.impl;

import com.alibaba.fastjson.JSON;
import com.yaorange.common.core.exception.ServiceException;
import com.yaorange.teaching.domain.YaHoliday;
import com.yaorange.teaching.domain.YaHolidayConfig;
import com.yaorange.teaching.domain.bo.YaHolidayBo;
import com.yaorange.teaching.mapper.YaHolidayConfigMapper;
import com.yaorange.teaching.mapper.YaHolidayMapper;
import com.yaorange.teaching.service.IYaHolidayConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 节假日配置Service业务层处理
 * 
 * @author SL
 * @date 2025-02-28
 */
@Service
public class YaHolidayConfigServiceImpl implements IYaHolidayConfigService 
{
    @Autowired
    private YaHolidayConfigMapper yaHolidayConfigMapper;
    @Autowired
    private YaHolidayMapper yaHolidayMapper;

    /**
     * 查询节假日配置
     * 
     * @param id 节假日配置主键
     * @return 节假日配置
     */
    @Override
    public YaHolidayConfig selectYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigById(id);
    }

    /**
     * 查询节假日配置列表
     * 
     * @param yaHolidayConfig 节假日配置
     * @return 节假日配置
     */
    @Override
    public List<YaHolidayConfig> selectYaHolidayConfigList(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigList(yaHolidayConfig);
    }

    /**
     * 新增节假日配置
     *
     * @param yaHolidayBoList 节假日配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertYaHolidayConfig(List<YaHolidayBo> yaHolidayBoList) {
        // 校验yaHolidayBoList不为null，且长度不小于7
        if (CollectionUtils.isEmpty(yaHolidayBoList) || yaHolidayBoList.size() < 7) {
            throw new ServiceException("节假日配置数据结构有问题！");
        }

        // 获取年份
        String year = yaHolidayBoList.get(6).getDate().substring(0, 4);

        // 删除已有的配置
        YaHolidayConfig tempYaConfig = yaHolidayConfigMapper.selectYaHolidayConfigByYear(year);
        if (tempYaConfig != null) {
            yaHolidayConfigMapper.deleteYaHolidayConfigByYear(year);
            yaHolidayMapper.deleteYaHolidayByConfigId(tempYaConfig.getId());
        }

        // 创建新的配置
        YaHolidayConfig yaHolidayConfig = new YaHolidayConfig();
        yaHolidayConfig.setYear(year);
        yaHolidayConfig.setContent(JSON.toJSONString(yaHolidayBoList));
        yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);

        // 准备批量插入的YaHoliday对象
        List<YaHoliday> yaHolidayList = new ArrayList<>();
        for (YaHolidayBo yaHolidayBo : yaHolidayBoList) {
            LocalDate localDate = LocalDate.parse(yaHolidayBo.getDate());
            for (int i = 0; i < yaHolidayBo.getCount(); i++) {
                YaHoliday yaHoliday = new YaHoliday();
                yaHoliday.setDate(localDate.plusDays(i).toString());
                yaHoliday.setWeekday(localDate.plusDays(i).getDayOfWeek().getValue());
                yaHoliday.setDescription(yaHolidayBo.getDescription());
                yaHoliday.setConfigId(yaHolidayConfig.getId());
                yaHolidayList.add(yaHoliday);
            }
        }

        // 批量插入YaHoliday对象
        return yaHolidayMapper.batchInsertYaHoliday(yaHolidayList);
    }


    /**
     * 修改节假日配置
     * 
     * @param yaHolidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int updateYaHolidayConfig(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.updateYaHolidayConfig(yaHolidayConfig);
    }

    /**
     * 批量删除节假日配置
     * 
     * @param ids 需要删除的节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigByIds(Long[] ids)
    {
        //同时删除节假日表中的数据
        yaHolidayMapper.deleteYaHolidayByConfigIds(ids);
        return yaHolidayConfigMapper.deleteYaHolidayConfigByIds(ids);
    }
}
